//
//  MainActivityView.m
//  Rabbit
//
//  Created by 阿帕 on 2018/11/3.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "MainActivityView.h"
#import "MainActivityCell.h"

@interface MainActivityView()<UITableViewDelegate , UITableViewDataSource>

/* collection */
@property (strong , nonatomic)UITableView *tableView;

@end

static NSString *const CYHomeActivityCellID = @"CYHomeActivityCell";

static const CGFloat cellHeight = 180;

@implementation MainActivityView

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200, 30)];
//    label.text = @"最新活动";
//    label.font = [UIFont boldSystemFontOfSize:15];
//    [self addSubview:label];
//
//    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, label.cy_bottom, 200, 15)];
//    label2.text = @"平台补贴优惠，机会不容错过";
//    label2.font = [UIFont systemFontOfSize:12];
//    label2.textColor = Light_TextColor;
//    [self addSubview:label2];
    
    self.backgroundColor = [UIColor whiteColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)];
    label.text = @"最新货源";
    label.font = font(20);
    label.textColor = [UIColor cy_colorWithHexString:@"#434C67"];
    [self addSubview:label];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(self.cy_right-80, 0, 80, 40);
    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn setTitle:@"更多" forState:UIControlStateNormal];
    [rightBtn setImage:image(@"gcy_icon_hd_rarrow") forState:UIControlStateNormal];
    rightBtn.titleLabel.font = font(13);
    [rightBtn addTarget:self action:@selector(moreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor cy_colorWithHexString:@"#96A0B9"] forState:UIControlStateNormal];
    [self addSubview:rightBtn];
    
    [rightBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
}

- (void)setActivityArray:(NSArray *)activityArray {
    
    _activityArray = activityArray;
    
    self.tableView.cy_height = cellHeight*activityArray.count;
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _activityArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CYHomeActivityCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.activityModel = _activityArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(activityIndexClick:)]) {
        [self.delegate activityIndexClick:indexPath.row];
    }
}

- (void)moreBtnClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(activityIndexClick:)]) {
        [self.delegate activityIndexClick:2000];
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        [self addSubview:_tableView];
        _tableView.frame = CGRectMake(0, 40, ScreenW, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"MainActivityCell" bundle:nil] forCellReuseIdentifier:CYHomeActivityCellID];
    }
    return _tableView;
}
@end
